package com.jeesite.modules.dataScreen.web;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.comparator.CompareUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateRange;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Filter;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.jeesite.modules.dataScreen.entity.GlysGdzdl;
import com.jeesite.modules.dataScreen.entity.GlysSlzzl;
import com.jeesite.modules.dataScreen.entity.Rcb;
import com.jeesite.modules.dataScreen.web.dto.TcrcbxqParam;
import com.jeesite.modules.dataScreen.web.vo.NameValueVo;
import com.jeesite.modules.dataScreen.web.vo.Result;
import com.jeesite.modules.dataScreen.web.vo.YunshuVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author: zhaosenlin
 * @date: 2024/5/14 14:08tc
 */
@RestController
@Api(tags = "运输接口")
@RequestMapping("/yunshu")
@Slf4j
public class YunshuApi extends BaseController {

    @ApiOperation("运输-江北机场")
    @GetMapping("jbjc")
    public Result<YunshuVo.Jbjc> jbjc() {
        Map<String, Object> fieldMean = new LinkedHashMap<>();
        fieldMean.put("title", "运输-江北机场");
        YunshuVo.Jbjc jcyj = new YunshuVo.Jbjc();
        fillDataByTiaoBao(allData(), YunshuVo.Jbjc.class, jcyj);

        String s = getTianbaoValue("江北机场航班正常率（准点率）及同比增速").orElse("");
        List<String> split = StrUtil.split(s, ",", true, true);
        jcyj.setValue5(Optional.ofNullable(CollUtil.getFirst(split)).orElse("-"));
        jcyj.setValue6(Optional.ofNullable(CollUtil.get(split, 1)).orElse("-"));

        String s2 = getTianbaoValue("江北机场靠桥率及同比增速").orElse("");
        List<String> split2 = StrUtil.split(s2, ",", true, true);
        jcyj.setValue3(Optional.ofNullable(CollUtil.getFirst(split2)).orElse("-"));

        fillFieldInfo(jcyj, fieldMean);
        return Result.OK(jcyj).apiInfo(fieldMean);
    }

    @ApiOperation("全部数据-测试用")
    @GetMapping("all")
    public Result<?> all() {
        String json = bwtJkzb();
        JSONArray jsonArray = JSONArray.parse(json);
        return Result.OK(jsonArray);
    }

    @ApiOperation("运输-预警")
    @GetMapping("yj")
    public Result<YunshuVo.A> yj() {
        Map<String, Object> fieldMean = new LinkedHashMap<>();
        fieldMean.put("title", "运输-预警");
        String json = bwtJkzb();
        JSONArray jsonArray = JSONArray.parse(json);
        YunshuVo.A a = new YunshuVo.A();
        a.setValue1(getValue(jsonArray, "火车站出租汽车候车乘客人车比（西站）", "-"));
        a.setValue2(getValue(jsonArray, "火车站出租汽车候车乘客人比（北站）", "-"));
        a.setValue3(getValue(jsonArray, "江北机场出租汽车候车客人比", "-"));
        a.setValue4(getValue(jsonArray, "火车站出租汽车候车乘客人比（沙坪坝站）", "-"));
        return Result.OK(a).apiInfo(fieldMean);
    }

    @ApiOperation("运输-轨道")
    @GetMapping("gd")
    public Result<YunshuVo.B> gd() {
        Map<String, Object> fieldMean = new LinkedHashMap<>();
        fieldMean.put("title", "运输-轨道");
        String json = bwtJkzb();
        JSONArray jsonArray = JSONArray.parse(json);

        YunshuVo.B b = new YunshuVo.B();
        b.setValue5(getValue(jsonArray, "轨道里程", "-"));
        b.setValue6(getValue(jsonArray, "车站数量", "-"));
        b.setValue7(getValue(jsonArray, "轨道运营车辆", "-"));
        b.setValue8(getValue(jsonArray, "轨道换乘车站数", "-"));
        b.setValue9(getValue(jsonArray, "轨道线路", "-"));
        b.setValue100(getTianbaoValue("轨道列车正点率").orElse("0"));

        // b.setValue10(ObjectUtil.defaultIfNull(sjtbDbDao.s_zc_fb_01(), "-"));
        // b.setValue10(NumberUtil.toStr(NumberUtil.mul(NumberUtil.toBigDecimal(b.getValue100()), new BigDecimal("100"))));
        b.setValue10(b.getValue100());
        b.setValue11(ObjectUtil.defaultIfNull(sjtbDbDao.s_zc_fb_02(), "-"));
        b.setValue99(ObjectUtil.defaultIfNull(sjtbDbDao.s_zc_fb_03(), "-"));

        b.setValue12(getValue(jsonArray, "轨道设施设备综合健康度", "-"));
        // b.setNameValues1(getValue(jsonArray, "昨日线路发送量前十排名", new ArrayList()));
        b.setNameValues1(new ArrayList<>());

        List<NameValueVo> top = new ArrayList<>();
        JSONObject tjXlrKyl = getTianbaoValue("轨道线路客流量").map(s -> {
            JSONArray jarr = JSON.parseArray(s);
            return (JSONObject) CollUtil.toMap(jarr, new JSONObject(),
                    o -> ((JSONObject) o).getString("line"),
                    o -> ((JSONObject) o).getString("line_ridership"));
        }).orElse(new JSONObject());
        top.add(new NameValueVo().setName("1号线").setValue(tjXlrKyl.getString("1")));
        top.add(new NameValueVo().setName("2号线").setValue(tjXlrKyl.getString("2")));
        top.add(new NameValueVo().setName("3号线").setValue(tjXlrKyl.getString("3")));
        top.add(new NameValueVo().setName("4号线").setValue(tjXlrKyl.getString("4")));
        top.add(new NameValueVo().setName("5号线").setValue(tjXlrKyl.getString("5")));
        top.add(new NameValueVo().setName("6号线").setValue(tjXlrKyl.getString("6")));
        top.add(new NameValueVo().setName("9号线").setValue(tjXlrKyl.getString("9")));
        top.add(new NameValueVo().setName("10号线").setValue(tjXlrKyl.getString("10")));
        top.add(new NameValueVo().setName("环线").setValue(tjXlrKyl.getString("98")));
        // top.add(new NameValueVo().setName("江跳线").setValue(tjXlrKyl.getString("line_jiangtiao")));
        top.add(new NameValueVo().setName("国博线").setValue(tjXlrKyl.getString("99")));
        CollUtil.sort(top, (o1, o2) -> CompareUtil.compare(
                NumberUtil.toBigDecimal(o2.getValue()),
                NumberUtil.toBigDecimal(o1.getValue()),
                false));
        b.setNameValues2(top);

        b.setValue101(getTianbaoValue("轨道列车服务可靠度").orElse("-"));
        b.setValue102(getTianbaoValue("轨道线网乘客满意度").orElse("-"));
        b.setValue104(getTianbaoValue("轨道站点100米公交站覆盖率").orElse("-"));
        b.setValue105(getTianbaoValue("轨道站点50米公交站覆盖率").orElse("-"));
        b.setValue107(getTianbaoValue("轨道线网客流总量").orElse("-"));
        b.setValue108(getTianbaoValue("轨道公交换乘客流").orElse("-"));
        b.setValue109(getTianbaoValue("轨道线网进站客运量").orElse("-"));
        b.setValue110(getTianbaoValue("轨道线网换乘客运量").orElse("-"));
        b.setValue111(getTianbaoValue("轨道线网历史最高客运量").orElse("-"));
        List<NameValueVo> value112 = getTianbaoValue("重点车站客运量")
                .map(o -> {
                    JSONObject jo = JSONObject.parseObject(o);
                    List<NameValueVo> voList = new ArrayList<>();
                    jo.forEach((k, v) -> {
                        voList.add(new NameValueVo(k, StrUtil.trim(StrUtil.toString(v))));
                    });
                    CollUtil.sort(voList, (o1, o2) -> CompareUtil.compare(
                            NumberUtil.toBigDecimal(o2.getValue()),
                            NumberUtil.toBigDecimal(o1.getValue()),
                            false));
                    return voList;
                })
                .orElse(new ArrayList<>());
        b.setValue112(value112);
        b.setValue113(getTianbaoValue("城市轨道交通客运分担率").orElse("-"));

        return Result.OK(b).apiInfo(fieldMean);
    }

    @ApiOperation("运输-出租车")
    @GetMapping("czc")
    public Result<YunshuVo.C> czc() {
        Map<String, Object> fieldMean = new LinkedHashMap<>();
        fieldMean.put("title", "运输-出租车");
        String json = bwtJkzb();
        JSONArray jsonArray = JSONArray.parse(json);

        YunshuVo.C c = new YunshuVo.C();
        c.setValue13(getValue(jsonArray, "中心城区巡游出租车运营业户数", "-"));
        c.setValue14(getValue(jsonArray, "巡游出租车上线运力", "-"));
        c.setValue15(getValue2(jsonArray, "中心城区巡游出租车行业稳定度", "-"));
        c.setValue16(getValue(jsonArray, "中心城区巡游出租车当日载客人次", "-"));
        c.setValue17(getValue(jsonArray, "中心城区巡游出租车当日车均营收收入（元）", "-"));
        // c.setValue18(getValue(jsonArray, "巡游出租车辆数", "-"));
        c.setValue18(getTianbaoValue("中心城区巡游出租车营运车辆数").orElse("-"));
        c.setValue19(getValue(jsonArray, "中心城区巡游出租车昨日订单数", "-"));
        c.setValue20(getValue(jsonArray, "火车站出租汽车候车乘客人车比（西站）", "-"));
        c.setValue21(getValue(jsonArray, "中心城区巡游出租车全天总订单数", "-"));
        c.setValue22(getValue(jsonArray, "中心城区巡游出租车当日平均载客次数", "-"));
        c.setValue23(getValue2(jsonArray, "中心城区网约出租车行业饱和度", "-"));
        c.setValue24(getValue(jsonArray, "全市网约出租车运营车辆数", "-"));
        c.setValue25(getValue(jsonArray, "全市网约出租车实时在线数", "-"));
        c.setValue100(getValue(jsonArray, "中心城区巡游出租车实时在线数", "-"));
        c.setValue26(getValue(jsonArray, "全市网约出租车客流量", "-"));
        c.setValue27(getValue(jsonArray, "全市网约出租车驾驶员当日平均营收", "-"));
        c.setValue28(getValue(jsonArray, "全市网约出租车运营平台数", "-"));
        c.setValue29(getValue(jsonArray, "全市网约出租车昨日订单总数", "-"));
        c.setValue30(getValue(jsonArray, "全市网约出租车当日单车平均订单数", "-"));
        c.setValue85(getValue2(jsonArray, "中心城区巡游出租车实载率", "-"));
        c.setValue86(getValue(jsonArray, "昨日全市网约出租车客流量", "-"));
        c.setValue88(getValue(jsonArray, "昨日中心城区巡游出租车客流量", "-"));

        return Result.OK(c).apiInfo(fieldMean);
    }

    @ApiOperation("运输-公交")
    @GetMapping("gj")
    public Result<YunshuVo.D> gj() {
        Map<String, Object> fieldMean = new LinkedHashMap<>();
        fieldMean.put("title", "运输-公交");
        String json = bwtJkzb();
        JSONArray jsonArray = JSONArray.parse(json);

        YunshuVo.D d = new YunshuVo.D();
        d.setValue31(getValue2(jsonArray, "中心城区公交运营车辆数", "-"));
        d.setValue32(getValue2(jsonArray, "中心城区昨日公交客流量", "-"));
        d.setValue33(getValue2(jsonArray, "中心城区公交线路数", "-"));
        d.setValue34(getValue2(jsonArray, "中心城区公交优先道里程", "-"));
        d.setValue35(getValue2(jsonArray, "中心城区公交线网长度", "-"));
        d.setValue36(getValue2(jsonArray, "中心城区公交运营站点数", "-"));
        d.setValue37(getValue(jsonArray, "中心城区昨日公交高峰平均发班间隔", "-"));
        d.setValue84(getValue(jsonArray, "中心城区昨日公交平均发班间隔", "-"));
        d.setValue83(getValue(jsonArray, "中心城区昨日公交高峰客流量", "-"));
        d.setValue82(getValue(jsonArray, "当日中心城区公交乘坐人次", "-"));
        d.setValue38(getValue(jsonArray, "中心城区工作日高峰公交上线率", "-"));
        d.setValue39(getValue(jsonArray, "中心城区工作日高峰每30分钟公交客流恢复率", "-"));
        d.setValue40(getValue(jsonArray, "中心城区公交平均车速", "-"));
        d.setValue41(getValue(jsonArray, "中心城区公交运营车辆数", "-"));
        d.setValue42(getValue(jsonArray, "中心城区公交上线率", "-"));
        d.setValue43(getValue(jsonArray, "本月中心城区公交出行人次较上月环比", "-"));
        d.setValue44(getValue(jsonArray, "中心城区小巷公交运营线路数", "-"));
        d.setValue45(getValue(jsonArray, "中心城区小巷公交运营车辆数", "-"));
        d.setValue46(getValue(jsonArray, "中心城区昨日小巷公交客流量", "-"));
        d.setValue47(getValue(jsonArray, "观光巴士运营里程", "-"));
        d.setValue48(getValue(jsonArray, "观光巴士运营线路数", "-"));
        d.setValue49(getValue(jsonArray, "观光巴士运营站点", "-"));
        d.setValue50(getValue(jsonArray, "观光巴士运营车辆", "-"));
        d.setValue51(getValue(jsonArray, "观光巴士订单量", "-"));
        d.setValue52(getValue(jsonArray, "观光巴士金额", "-"));
        d.setValue86(getValue(jsonArray, "中心城区昨日公交线路客流量", new ArrayList()));
        d.setValue89(getValue(jsonArray, "观光巴士运营线路数", "-"));
        d.setValue90(getValue(jsonArray, "运营车辆分析饼图", new ArrayList()));
        d.setValue91(getValue(jsonArray, "运营线路分析饼图", new ArrayList()));
        d.setValue92(getValue(jsonArray, "中心城区公交上线率", "-"));
        d.setValue93(getValue(jsonArray, "恢复率柱状图", new ArrayList()));
        d.setValue94(getTianbaoValue("中心城区公交线路接驳轨道比例").orElse("-"));
        d.setValue95(getTianbaoValue("中心城区微循环公交线路接驳轨道比例").orElse("-"));
        d.setValue96(getTianbaoValue("中心城区工作日早高峰上线率").orElse("-"));

        return Result.OK(d).apiInfo(fieldMean);
    }

    @ApiOperation("运输-班线客运")
    @GetMapping("bxky")
    public Result<YunshuVo.E> bxky() {
        Map<String, Object> fieldMean = new LinkedHashMap<>();
        fieldMean.put("title", "运输-班线客运");

        String json = bwtJkzb();
        JSONArray jsonArray = JSONArray.parse(json);
        YunshuVo.E e = new YunshuVo.E();
        e.setValue53(getValue2(jsonArray, "全市当日班线客运车辆数量", "-"));
        e.setValue54(getValue2(jsonArray, "全市当日班线客运企业数量", "-"));
        e.setValue55(getValue2(jsonArray, "全市当日班线客运运营线路总数", "-"));
        e.setValue56(getValue(jsonArray, "班线客运当日发班次数", "-"));
        e.setValue57(getValue(jsonArray, "当日全市班车包车运力总饱和度", "-"));
        e.setValue58(getValue(jsonArray, "班线客运当日累计购票量", "-"));
        e.setValue59(getValue(jsonArray, "班线客运当日实载率", "-"));
        e.setValue60(getValue2(jsonArray, "全市当日联网售票班线客运跨省发送量占比", ""));

        if (StrUtil.isNotBlank((String) e.getValue60())) {
            ArrayList<Map.Entry<String, String>> entries = new ArrayList<>(getStringStringMap((String) e.getValue60()).entrySet());
            entries.sort(new Comparator<Map.Entry<String, String>>() {
                @Override
                public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {
                    return CompareUtil.compare(
                            NumberUtil.toBigDecimal((String) o2.getValue()),
                            NumberUtil.toBigDecimal((String) o1.getValue()), false);
                }
            });
            Map<String, Object> sortedMap = new LinkedHashMap<>();
            for (Map.Entry<String, String> entry : entries) {
                sortedMap.put(entry.getKey(), entry.getValue());
            }
            e.setValue60(sortedMap);
        }

        e.setValue61(getValue(jsonArray, "当日定制客运量占比", "-"));
        e.setValue62(getValue2(jsonArray, "全市当日联网售票市内班线客运客流量占比","-"));
        // e.setValue63(getValue(jsonArray, "全市月班线客运量", "-"));
        e.setValue64(getValue2(jsonArray, "全市联网售票班线客运当日累计客流量", "-"));
        e.setValue65(getValue(jsonArray, "班线客运昨日发班次数", "-"));
        e.setValue66(getValue(jsonArray, "班线客运昨日客流量", "-"));

        e.setNameValues1(getSplit(getValue2(jsonArray, "全市昨日线路发送量前十排名", "")));
        e.setNameValues2(getSplit(getValue2(jsonArray, "全市昨日车站发送量前十排名", "")));

        e.setValue1(getTianbaoValue("全市汽车客运站客流预警").orElse("-"));
        e.setValue2(getTianbaoValue("全市当日重点汽车客运站客流量饱和率").orElse("-"));

        return Result.OK(e).apiInfo(fieldMean);
    }

    @ApiOperation("运输-货运")
    @GetMapping("hy")
    public Result<YunshuVo.F> hy() {
        Map<String, Object> fieldMean = new LinkedHashMap<>();
        fieldMean.put("title", "运输-货运");
        String json = bwtJkzb();
        JSONArray jsonArray = JSONArray.parse(json);

        YunshuVo.F f = new YunshuVo.F();
        JSONObject sjwTransportDataTotal = sjtbDbDao.ads_sjw_transport_data_total();
        f.setValue67(safeGet(sjwTransportDataTotal, o -> o.getString("freight_volumef_year"), "-"));
        f.setValue68(getValue(jsonArray, "道路货运周转量", "-"));
        f.setValue69(safeGet(sjwTransportDataTotal, o -> o.getString("veh"), "-"));
        f.setValue70(safeGet(sjwTransportDataTotal, o -> o.getString("owner"), "-"));
        // f.setValue71(getValue(jsonArray, "教练车实时在线率", "-"));
        f.setValue71(getTianbaoValue("教练车实时在线率").orElse("-"));
        f.setValue72(getValue(jsonArray, "驾校学员教练车人车比", "-"));
        // f.setValue73(getValue(jsonArray, "学员实时在线率", "-"));
        f.setValue73(getTianbaoValue("教练车实时培训率").orElse("-"));
        f.setValue74(getValue(jsonArray, "全市排放超标车辆进M站数量", "-"));
        // f.setValue75(getValue(jsonArray, "全市驾校数量", "-"));
        f.setValue75(getTianbaoValue("全市驾驶培训机构数量").orElse("-"));
        f.setValue76(getValue(jsonArray, "全市尾气治理站（M站）数量", "-"));
        f.setValue77(getValue(jsonArray, "驾校综合评分", "-"));
        // f.setValue78(getValue(jsonArray, "驾校学员存量数据", "-"));
        f.setValue78(getTianbaoValue("驾校学员存量数据").orElse("-"));
        f.setValue79(getValue(jsonArray, "全市超标排放治理合格率", "-"));
        f.setValue80(getValue(jsonArray, "当月学员考试合格率", "-"));
        // f.setValue81(getValue(jsonArray, "全市一二类和重点三类汽车维修企业车辆维修数量", "-"));
        f.setValue81(getTianbaoValue("一二类和重点三类维修企业维修次数").orElse("-"));
        // f.setValue88(getValue(jsonArray, "全市一二类和重点三类汽车维修企业数量", "-"));
        f.setValue88(getTianbaoValue("一二类和重点三类维修企业数量").orElse("-"));
        // f.setValue89(getValue(jsonArray, "当月学员考试合格率-科目一考试通过率", "-"));
        f.setValue89(getTianbaoValue("科目一考试通过率").orElse("-"));
        // f.setValue90(getValue(jsonArray, "当月学员考试合格率-科目二考试通过率", "-"));
        f.setValue90(getTianbaoValue("科目二考试通过率").orElse("-"));
        // f.setValue91(getValue(jsonArray, "当月学员考试合格率-科目三考试通过率", "-"));
        f.setValue91(getTianbaoValue("科目三考试通过率").orElse("-"));

        return Result.OK(f).apiInfo(fieldMean);
    }

    @ApiOperation("轨道正点率")
    @GetMapping("gdzdl")
    public Result<Page<GlysGdzdl>> gdzdl(
            @ApiParam("页码") @RequestParam(required = false, defaultValue = "1") Integer page,
            @ApiParam("数据条数") @RequestParam(required = false, defaultValue = "10") Integer limit) {
        Page<GlysGdzdl> glysGdzdlPage = themeDbDao.glysGdzdl(Page.of(page, limit));
        return Result.OK(glysGdzdlPage);
    }

    @ApiOperation("枢纽场站旅客滞留")
    @GetMapping("snzczllk")
    public Result<Page<GlysSlzzl>> snzczllk(
            @ApiParam("页码") @RequestParam(required = false, defaultValue = "1") Integer page,
            @ApiParam("数据条数") @RequestParam(required = false, defaultValue = "10") Integer limit) {
        Page<GlysSlzzl> glysGdzdlPage = themeDbDao.glysSlzzl(Page.of(page, limit));
        return Result.OK(glysGdzdlPage);
    }

    @ApiOperation("多天数据集合，截至到昨天")
    @GetMapping("dbfx")
    public Result<List<YunshuVo.DaysData>> dbfx(@ApiParam("条数限制") @RequestParam(required = false, defaultValue = "7") Integer limit) {
        String day = DateUtil.format(DateUtil.date(), "yyyy-MM-dd");
        List<NameValueVo> nameValueVos = sjtbDbDao.daysData(day, limit);
        // List<NameValueVo>       lcsxsDaysData = sjtbDbDao.lcsxsDaysData(day, limit);
        List<YunshuVo.DaysData> daysDataList = new ArrayList<>();
        for (NameValueVo nameValueVo : nameValueVos) {
            String curDay = nameValueVo.getName();
            JSONArray jsonArray = JSONArray.parse(nameValueVo.getValue());
            YunshuVo.DaysData daysData = new YunshuVo.DaysData();
            daysData.setV1(getValue(jsonArray, "中心城区巡游出租车当日车均营收收入（元）", "-"));
            daysData.setV2(getValue(jsonArray, "全市网约出租车驾驶员当日平均营收", "-"));
            // daysData.setV3(getValue(jsonArray, "全市网约出租车当日累计订单总数", "-"));
            daysData.setV3(getValue(jsonArray, "全市网约出租车昨日订单总数", "-"));
            // daysData.setV4(getValue(jsonArray, "中心城区巡游出租车全天总订单数", "-"));
            daysData.setV4(getValue(jsonArray, "中心城区巡游出租车昨日订单数", "-"));

            daysData.setV6(getValue(jsonArray, "全市一二类和重点三类汽车维修企业车辆维修数量", "-"));
            daysData.setV7(getValue(jsonArray, "当日全市班车包车运力总饱和度", "-"));
            daysData.setV8(getValue(jsonArray, "班线客运当日实载率", "-"));
            daysData.setV9(getValue(jsonArray, "班线客运当日实载率", "-"));

            daysData.setRcjyr(getValue(jsonArray, "中心城区巡游出租车当日车均营收收入（元）", "-"));
            daysData.setRcqkl(getValue(jsonArray, "中心城区巡游出租车日出勤率", "-"));
            daysData.setZklllyl(getValue(jsonArray, "中心城区巡游出租车载客里程利用率", "-"));

            // NameValueVo one = CollUtil.findOne(lcsxsDaysData, nameValueVo1 -> StrUtil.equals(curDay, nameValueVo1.getName()));
            // daysData.setV5(safeGet(one, NameValueVo::getValue, "-"));
            daysData.setDay(nameValueVo.getName());
            daysDataList.add(daysData);
        }

        daysDataList.sort(Comparator.comparing(YunshuVo.DaysData::getDay));

        return Result.OK(daysDataList);
    }

    @ApiOperation("多天数据集合，截至到今天")
    @GetMapping("lcsxs")
    public Result<List<YunshuVo.DaysData>> lcsxs(@ApiParam("条数限制") @RequestParam(required = false, defaultValue = "7") Integer limit) {
        // 为了包括今天的数据，往后偏移一天
        String day = DateUtil.format(DateUtil.offsetDay(DateUtil.date(), 1), "yyyy-MM-dd");
        List<NameValueVo> nameValueVos = sjtbDbDao.daysData(day, limit);
        List<NameValueVo> lcsxsDaysData = sjtbDbDao.lcsxsDaysData(day, limit);
        List<YunshuVo.DaysData> daysDataList = new ArrayList<>();
        for (NameValueVo nameValueVo : nameValueVos) {
            String curDay = nameValueVo.getName();
            JSONArray jsonArray = JSONArray.parse(nameValueVo.getValue());
            YunshuVo.DaysData daysData = new YunshuVo.DaysData();
            daysData.setV1(getValue(jsonArray, "中心城区巡游出租车当日车均营收收入（元）", "-"));
            daysData.setV2(getValue(jsonArray, "全市网约出租车驾驶员当日平均营收", "-"));
            // daysData.setV3(getValue(jsonArray, "全市网约出租车当日累计订单总数", "-"));
            daysData.setV3(getValue(jsonArray, "全市网约出租车昨日订单总数", "-"));
            // daysData.setV4(getValue(jsonArray, "中心城区巡游出租车全天总订单数", "-"));
            daysData.setV4(getValue(jsonArray, "中心城区巡游出租车昨日订单数", "-"));

            daysData.setV6(getValue(jsonArray, "全市一二类和重点三类汽车维修企业车辆维修数量", "-"));
            daysData.setV7(getValue(jsonArray, "当日全市班车包车运力总饱和度", "-"));
            daysData.setV8(getValue(jsonArray, "班线客运当日实载率", "-"));
            daysData.setV9(getValue(jsonArray, "班线客运当日实载率", "-"));

            daysData.setRcjyr(getValue(jsonArray, "中心城区巡游出租车当日车均营收收入（元）", "-"));
            daysData.setRcqkl(getValue(jsonArray, "中心城区巡游出租车日出勤率", "-"));
            daysData.setZklllyl(getValue(jsonArray, "中心城区巡游出租车载客里程利用率", "-"));

            NameValueVo one = CollUtil.findOne(lcsxsDaysData, nameValueVo1 -> StrUtil.equals(curDay, nameValueVo1.getName()));
            daysData.setV5(safeGet(one, NameValueVo::getValue, "-"));
            daysData.setDay(nameValueVo.getName());
            daysDataList.add(daysData);
        }

        daysDataList.sort(Comparator.comparing(YunshuVo.DaysData::getDay));

        return Result.OK(daysDataList);
    }


    @ApiOperation("小时数据集合")
    @GetMapping("xsdata")
    public Result<List<YunshuVo.HoursData>> xsdata(@ApiParam(value = "limit", defaultValue = "7") Integer limit) {
        DateTime now = DateUtil.date();
        String day = DateUtil.format(now, "yyyy-MM-dd 00");
        List<NameValueVo> nameValueVos = sjtbDbDao.hoursData(day);
        List<YunshuVo.HoursData> daysDataList = new ArrayList<>();
        Map<String, NameValueVo> map = CollUtil.toMap(nameValueVos, new HashMap<>(), NameValueVo::getName);

        String vv = getValue2(null, "中心城区当日公交乘坐人次", "[]");
        JSONArray arr = JSON.parseArray(vv);

        // 从0点开始遍历，到现在时刻
        for (DateTime t : new DateRange(now.setField(DateField.HOUR_OF_DAY, 6), DateUtil.date(), DateField.HOUR_OF_DAY)) {
            String tStr = DateUtil.format(t, "yyyy-MM-dd HH");
            // 获取小时对应的值，为null用默认值
            NameValueVo nameValueVo = map.getOrDefault(tStr, new NameValueVo().setName(tStr).setValue("[]"));
            JSONArray jsonArray = JSONArray.parse(nameValueVo.getValue());
            YunshuVo.HoursData hoursData = new YunshuVo.HoursData();

            // hoursData.setV1(getValue(jsonArray, "当日中心城区公交乘坐人次", "-"));
            hoursData.setV1(Optional.ofNullable(CollUtil.findOne(arr, o ->
                            DateUtil.format(t, "HH").equals(((JSONObject) o).getString("name"))))
                    .map(o -> ((JSONObject) o).getString("value")).orElse("-"));
            hoursData.setV2(getValue(jsonArray, "中心城区工作日高峰每30分钟公交客流恢复率", "-"));
            hoursData.setV3(getValue(jsonArray, "中心城区公交上线率", "-"));

            hoursData.setDayHour(nameValueVo.getName() + ":00:00");
            daysDataList.add(hoursData);
        }

        return Result.OK(daysDataList);
    }

    @ApiOperation("弹窗-中心城区巡游出租车实载率")
    @GetMapping("tcxsl")
    public Result<List<NameValueVo>> tcxsl() {
        DateTime now = DateUtil.date();
        String day = DateUtil.format(now, "yyyy-MM-dd 00");
        List<NameValueVo> nameValueVos = sjtbDbDao.hoursData(day);
        Map<String, NameValueVo> map = CollUtil.toMap(nameValueVos, new HashMap<>(), NameValueVo::getName);
        List<NameValueVo> nameValueVoList = new ArrayList<>();

        // 从0点开始遍历，到现在时刻
        for (DateTime t : new DateRange(now.setField(DateField.HOUR_OF_DAY, 6), DateUtil.date(), DateField.HOUR_OF_DAY)) {
            String tStr = DateUtil.format(t, "yyyy-MM-dd HH");
            // 获取小时对应的值，为null用默认值
            NameValueVo nameValueVo = map.getOrDefault(tStr, new NameValueVo().setName(tStr).setValue("[]"));
            JSONArray jsonArray = JSONArray.parse(nameValueVo.getValue());

            nameValueVoList.add(new NameValueVo()
                    .setName(nameValueVo.getName() + ":00:00")
                    .setValue(getValue(jsonArray, "中心城区巡游出租车实载率", "-")));
        }

        return Result.OK(nameValueVoList);
    }

    @ApiOperation("中心城区巡游车昨日客流总量")
    @GetMapping("tcdds")
    public Result<List<NameValueVo>> tcdds() {
        String json = bwtJkzb();
        JSONArray jsonArray = JSONArray.parse(json);

        ArrayList<Object> value = getValue(jsonArray, "中心城区巡游出租车昨日各区（县）订单数", new ArrayList<>());

        List<NameValueVo> map1 = CollUtil.map(value, new Function<Object, NameValueVo>() {
            @Override
            public NameValueVo apply(Object o) {
                JSONObject jo = (JSONObject) o;
                return new NameValueVo()
                        .setName(jo.getString("区县名称"))
                        .setValue(jo.getString("订单数"));
            }
        }, true);

        CollUtil.filter(map1, new Filter<NameValueVo>() {
            @Override
            public boolean accept(NameValueVo nameValueVo) {
                return CollUtil.contains(Lists.newArrayList(
                        "渝中区", "江北区", "南岸区", "九龙坡区", "沙坪坝区", "大渡口区", "北碚区", "渝北区", "巴南区"
                ), nameValueVo.getName());
            }
        });

        CollUtil.sort(map1, (o1, o2) -> CompareUtil.compare(
                NumberUtil.toBigDecimal(o2.getValue()),
                NumberUtil.toBigDecimal(o1.getValue()),
                false));

        return Result.OK(map1);
    }

    @ApiOperation("弹窗-昨客流量")
    @GetMapping("tczkll")
    public Result<YunshuVo.BB> tczkll() {
        String json = bwtJkzb();
        JSONArray jsonArray = JSONArray.parse(json);

        ArrayList<Object> value = getValue(jsonArray, "中心城区昨日公交线路客流量明细", new ArrayList<>());
        ArrayList<Object> value1 = getValue(jsonArray, "中心城区昨日公交站点客流量明细", new ArrayList<>());

        List<NameValueVo> map1 = CollUtil.map(value, new Function<Object, NameValueVo>() {
            @Override
            public NameValueVo apply(Object o) {
                JSONObject jo = (JSONObject) o;
                return new NameValueVo()
                        .setName(StrUtil.removePrefix(jo.getString("线路编号"), "000"))
                        .setValue(jo.getString("线路客流量"));
            }
        }, true);
        List<NameValueVo> map2 = CollUtil.map(value1, new Function<Object, NameValueVo>() {
            @Override
            public NameValueVo apply(Object o) {
                JSONObject jo = (JSONObject) o;
                return new NameValueVo()
                        .setName(jo.getString("站点名称"))
                        .setValue(jo.getString("站点客流量"));
            }
        }, true);

        YunshuVo.BB bb = new YunshuVo.BB();
        bb.setTop10xlkll(top(map1, 10));
        bb.setTop10czkll(top(map2, 10));
        return Result.OK(bb);
    }

    @ApiOperation("弹窗-全市网约出租车总订单数")
    @GetMapping("tczjd")
    public Result<?> tczjd() {
        String json = bwtJkzb();
        JSONArray jsonArray = JSONArray.parse(json);

        ArrayList<Object> value = getValue(jsonArray, "当日全市网约车订单前十平台", new ArrayList<>());
        return Result.OK(value);
    }

    @ApiOperation("弹窗-全市网约出租车昨日各区（县）订单数")
    @GetMapping("tcqzjd")
    public Result<?> tcqzjd() {
        String json = bwtJkzb();
        JSONArray jsonArray = JSONArray.parse(json);

        ArrayList<Object> value = getValue(jsonArray, "全市网约出租车昨日各区（县）订单数", new ArrayList<>());
        return Result.OK(value);
    }

    @ApiOperation("弹窗-全市网约出租车平台许可明细")
    @GetMapping("tcxkmx")
    public Result<?> tcxkmx() {
        String json = bwtJkzb();
        JSONArray jsonArray = JSONArray.parse(json);

        ArrayList<Object> value = getValue(jsonArray, "全市网约出租车平台许可明细", new ArrayList<>());
        return Result.OK(value);
    }

    @ApiOperation("弹窗-中心城区巡游出租车")
    @GetMapping("tcxycz")
    public Result<YunshuVo.AA> tcxycz() {
        String json = bwtJkzb();
        JSONArray jsonArray = JSONArray.parse(json);

        YunshuVo.AA aa = new YunshuVo.AA();
        aa.setRcjyr(getValue(jsonArray, "中心城区巡游出租车当日车均营收收入（元）", "0"));
        aa.setRcqkl(getValue(jsonArray, "中心城区巡游出租车日出勤率", "0"));
        aa.setZklllyl(getValue(jsonArray, "中心城区巡游出租车载客里程利用率", "0"));

        return Result.OK(aa);
    }

    @ApiOperation("弹窗-全市网约出租车行业饱和度详情")
    @GetMapping("tcbhdxq")
    public Result<YunshuVo.CC> tcbhdxq() {
        String json = bwtJkzb();
        JSONArray jsonArray = JSONArray.parse(json);

        YunshuVo.CC cc = new YunshuVo.CC();
        cc.setValue7(getValue(jsonArray, "中心城区网约车月均接单出勤率", "0"));
        cc.setValue8(getValue(jsonArray, "中心城区网约车单车日均接单量降幅", "0"));
        cc.setValue9(getValue(jsonArray, "中心城区网约车单车日均营收降幅", "0"));

        return Result.OK(cc);
    }

    @ApiOperation("全市月班线客运量")
    @GetMapping("qsybxkyl")
    public Result<Object> qsybxkyl() {
        String json = bwtJkzb();
        JSONArray jsonArray = JSONArray.parse(json);

        return Result.OK(getValue(jsonArray, "全市月班线客运量", new ArrayList<>()));
    }

    @ApiOperation("网约车行业饱和度-健康度")
    @GetMapping("wycbhd")
    public Result<List<YunshuVo.CC>> wycbhd() {
        DateTime start = DateUtil.beginOfDay(DateUtil.beginOfMonth(DateUtil.date().setField(DateField.MONTH, 0)));
        DateTime end = DateUtil.endOfDay(DateUtil.endOfMonth(DateUtil.date().setField(DateField.MONTH, 11)));

        List<NameValueVo> nameValueVos = sjtbDbDao.monthsData(DateUtil.formatDateTime(start), DateUtil.formatDateTime(end));

        Map<String, NameValueVo> map = CollUtil.toMap(nameValueVos, new HashMap<>(), NameValueVo::getName);

        List<YunshuVo.CC> data = new ArrayList<>();

        for (DateTime month : new DateRange(start, end, DateField.MONTH)) {
            String tStr = DateUtil.format(month, "yyyy-MM");

            NameValueVo nameValueVo = map.getOrDefault(tStr, new NameValueVo().setName(tStr).setValue("[]"));
            JSONArray jsonArray = JSONArray.parse(nameValueVo.getValue());
            YunshuVo.CC cc = new YunshuVo.CC();

            cc.setValue7(getValue(jsonArray, "中心城区网约车月均接单出勤率", "0"));
            cc.setValue8(getValue(jsonArray, "中心城区网约车单车日均接单量降幅", "0"));
            cc.setValue9(getValue(jsonArray, "中心城区网约车单车日均营收降幅", "0"));
            cc.setMonth(tStr);

            data.add(cc);
        }

        return Result.OK(data);
    }

    @ApiOperation("人车比下钻列表")
    @GetMapping("tcrcb")
    public Result<Page<Rcb>> tcrcb(
            @ApiParam("页码") @RequestParam(required = false, defaultValue = "1") Integer page,
            @ApiParam("数据条数") @RequestParam(required = false, defaultValue = "10") Integer limit) {
        Page<Rcb> rcbPage = themeDbDao.jtys_rcbgk(Page.of(page, limit));
        return Result.OK(rcbPage);
    }

    @ApiOperation("人车比下钻详情")
    @PostMapping("tcrcbxq")
    public Result<Page<Rcb>> tcrcbxq(
            @ApiParam("页码") @RequestParam(required = false, defaultValue = "1") Integer page,
            @ApiParam("数据条数") @RequestParam(required = false, defaultValue = "10") Integer limit,
            @RequestBody TcrcbxqParam param) {
        Page<Rcb> rcbPage = themeDbDao.jtys_rcbgkxq(Page.of(page, limit), param.getSjName(), param.getSjContent());
        return Result.OK(rcbPage);
    }

    @ApiOperation("一二类和重点三类维修企业维修次数")
    @GetMapping("wxcs")
    public Result<?> wxcs() {
        return Result.OK(JSON.parseArray(getTianbaoValue("一二类和重点三类维修企业维修次数").orElse("[]")));
    }

    public static void main(String[] args) {
        String b = "        上海市 0.08400 25|云南省 0.42200 125|北京市 0.10500 31|四川省 9.35100 2769|安徽省 0.00700 2|山东省 0.03400 10|广东省 1.25300 371|江苏省 0.25000 74|江西省 0.00300 1|河北省 0.04400 13|河南省 0.00300 1|浙江省 0.49300 146|湖北省 1.12500 333|湖南省 0.30700 91|福建省 0.76300 226|贵州省 1.58000 468|陕西省 0.26300 78 ";
        Map<String, String> map = getStringStringMap(b);
        System.out.println(map);
    }

    private static Map<String, String> getStringStringMap(String b) {
        List<String> split = getSplit(b);

        List<NameValueVo> collect = split.stream()
                .map(new Function<String, NameValueVo>() {
                    @Override
                    public NameValueVo apply(String s) {
                        List<String> ss = StrUtil.split(s, " ", true, false);
                        return new NameValueVo()
                                .setName(CollUtil.get(ss, 0))
                                .setValue(CollUtil.get(ss, 1));
                    }
                }).collect(Collectors.toList());

        Map<String, String> map = CollUtil.toMap(collect, new HashMap<>(), NameValueVo::getName, NameValueVo::getValue);
        return map;
    }

    private static List<String> getSplit(String b) {
        return StrUtil.split(b, "|", true, true);
    }
}
